package com.ruoyi.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.domain.AcBlog;
import com.ruoyi.blog.domain.AcLike;
import com.ruoyi.blog.service.IAcBlogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.AcCollection;
import com.ruoyi.blog.service.IAcCollectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收藏管理Controller
 * 
 * @author ruoyi
 * @date 2022-06-19
 */
@RestController
@RequestMapping("/blog/collection")
public class AcCollectionController extends BaseController
{
    @Autowired
    private IAcCollectionService acCollectionService;

    @Autowired
    private IAcBlogService acBlogService;

    @PostMapping("/collect/{id}")
    public AjaxResult collect(@PathVariable("id") Long blogId)
    {
        AcCollection collection = new AcCollection();
        collection.setUserId(getUserId());
        collection.setBlogId(blogId);
        return toAjax(acCollectionService.insertAcCollection(collection));
    }

    @PostMapping("/cancelCollect/{id}")
    public AjaxResult cancelCollect(@PathVariable("id") Long blogId)
    {
        AcCollection collection = new AcCollection();
        collection.setUserId(getUserId());
        collection.setBlogId(blogId);
        List<AcCollection> list = acCollectionService.selectAcCollectionList(collection);
        if (list.size() == 0)
            return error();
        collection = list.get(0);
        return toAjax(acCollectionService.deleteAcCollectionById(collection.getId()));
    }

    @GetMapping("/getCollectionList")
    public AjaxResult getCollectionList()
    {
        AcCollection collection = new AcCollection();
        collection.setUserId(getUserId());
        List<AcCollection> list = acCollectionService.selectAcCollectionList(collection);
        for (AcCollection c : list) {
            AcBlog blog = acBlogService.selectAcBlogById(c.getBlogId());
            blog.setContent(null);
            c.setBlog(blog);
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询收藏管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcCollection acCollection)
    {
        startPage();
        List<AcCollection> list = acCollectionService.selectAcCollectionList(acCollection);
        return getDataTable(list);
    }

    /**
     * 导出收藏管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:collection:export')")
    @Log(title = "收藏管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcCollection acCollection)
    {
        List<AcCollection> list = acCollectionService.selectAcCollectionList(acCollection);
        ExcelUtil<AcCollection> util = new ExcelUtil<AcCollection>(AcCollection.class);
        util.exportExcel(response, list, "收藏管理数据");
    }

    /**
     * 获取收藏管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:collection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(acCollectionService.selectAcCollectionById(id));
    }

    /**
     * 新增收藏管理
     */
    @PreAuthorize("@ss.hasPermi('blog:collection:add')")
    @Log(title = "收藏管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcCollection acCollection)
    {
        return toAjax(acCollectionService.insertAcCollection(acCollection));
    }

    /**
     * 修改收藏管理
     */
    @PreAuthorize("@ss.hasPermi('blog:collection:edit')")
    @Log(title = "收藏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcCollection acCollection)
    {
        return toAjax(acCollectionService.updateAcCollection(acCollection));
    }

    /**
     * 删除收藏管理
     */
    @PreAuthorize("@ss.hasPermi('blog:collection:remove')")
    @Log(title = "收藏管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acCollectionService.deleteAcCollectionByIds(ids));
    }
}
